//
//  myTabBarViewController.m
//  CustomTabBar
//
//  Created by giantumbrella on 15/5/20.
//  Copyright (c) 2015年 zhaojuan. All rights reserved.
//

#import "myTabBarViewController.h"
#import "HomeViewController.h"
//#import "HelpMeBagHomeVC.h"
//#import "HelpMeDoHomeVC.h"
#import "InformationViewController.h"//资讯页面
#import "MeViewController.h"

@interface myTabBarViewController ()

@end

@implementation myTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor whiteColor];
    [self setupAllChildViewControllers];
   
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"indexTabbar"] isEqualToString:@"3"]) {
        [self logSucess];
    }
}

- (void)logSucess
{
//    self.selectedIndex = 3;
    self.selectedIndex = 2;
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


#pragma mark---- 进入绘制 图案页面
- (void) popToDrawImageViewView
{
    //    [self.tabBarController setSelectedIndex:3];
    
    UIView *view_second;//第二个 重置密码背景
    //第二个 view的背景
    view_second = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:view_second];
    view_second.backgroundColor = COLOR_EVERYVIEWBG;
    
    //navigation
    UIView *view_navgation = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [view_second addSubview:view_navgation];
    view_navgation.backgroundColor = RXDColor(50, 68, 78);
    
    //back button
    UIButton *button_back = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 40, 30)];
    [view_navgation addSubview:button_back];
    [button_back setImage:[UIImage imageNamed:@"return_left"] forState:UIControlStateNormal];
    
    //        [button_back addTarget:self action:@selector(hideResetViewShowInputPassView) forControlEvents:UIControlEventTouchUpInside];
    
    //提示文字
    UILabel * label_tips = [[UILabel alloc] initWithFrame:CGRectMake(30*PROPORTION_WIDTH, 64+124, kScreenWidth-30*PROPORTION_WIDTH*2,30*PROPORTION_WIDTH)];
    [view_second addSubview:label_tips];
    label_tips.textAlignment = NSTextAlignmentCenter;
    label_tips.textColor = COLOT_TEXTGRAY;
    label_tips.font = [UIFont systemFontOfSize:Font_SIZE_24];
    
    [DBGuestureLock clearGuestureLockPassword]; //just for test
    
    //Working with block:
    DBGuestureLock *lock = [DBGuestureLock lockOnView:self.view onPasswordSet:^(DBGuestureLock *lock, NSString *password) {
        if (lock.firstTimeSetupPassword == nil) {
            lock.firstTimeSetupPassword = password;
            NSLog(@"varify your password");
            label_tips.text = @"请再次绘制解锁图案";
        }
    } onGetCorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        if (lock.firstTimeSetupPassword && ![lock.firstTimeSetupPassword isEqualToString:DBFirstTimeSetupPassword]) {
            lock.firstTimeSetupPassword = DBFirstTimeSetupPassword;
            NSLog(@"password has been setup!");
            [UIView showAlertMsg:@"重置密码成功"];
            [view_second removeFromSuperview];
            
        } else {
            NSLog(@"login success");
            
            //                [self.view_lockBG removeFromSuperview];
            
        }
    } onGetIncorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        if (![lock.firstTimeSetupPassword isEqualToString:DBFirstTimeSetupPassword]) {
            NSLog(@"Error: password not equal to first setup!");
            label_tips.text = @"两次绘制不一样";
            //两次密码不一致则重头开始
            [DBGuestureLock clearGuestureLockPassword]; //just for test
            lock.firstTimeSetupPassword = nil;
            
        } else {
            NSLog(@"login failed");
        }
    }];
    
    [view_second addSubview:lock];
    
    label_tips.text = @"请重置您的手势密码";
}

-(void)setupAllChildViewControllers{
    
    //1.首页
    HomeViewController *trialView = [[HomeViewController alloc] init];
    [self setupChildViewController:trialView title:@"MR帮" imageName:@"home" selectedImageName:@"home_ed"];
    
    //2.品牌
    InformationViewController *establishView = [[InformationViewController alloc]init];
    [self setupChildViewController:establishView title:@"资讯" imageName:@"zixun" selectedImageName:@"zixun_ed"];
    
     //3.购物车
//    HelpMeDoHomeVC *homeView = [[HelpMeDoHomeVC alloc] init];
//    [self setupChildViewController:homeView title:@"帮我办" imageName:@"tab_3" selectedImageName:@"tab_3_3"];
    
    //4.我
    MeViewController *meView = [[MeViewController alloc] init];
    [self setupChildViewController:meView title:@"我" imageName:@"tab_4" selectedImageName:@"tab_4_4"];
}

//初始化一个子控件
- (void)setupChildViewController:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{

//1.设置控件属性
    
    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    
    childVC.title = title;
    
    if ([[[UIDevice currentDevice] systemVersion]floatValue] >= 7.0) {
    
        childVC.tabBarItem.image = [[UIImage imageNamed:imageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    } else {
       
        //TBD
    }

    //2.包装一个导航栏
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:childVC];
    [self addChildViewController:navi];
}


@end
